package com.applink.sell_proof.builder;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import android.content.Context;
import android.content.Intent;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TableRow;
import android.widget.TextView;

import com.applink.sell_proof.DetailProject;
import com.applink.sell_proof.R;
import com.applink.sell_proof.model.Project;
import com.applink.sell_proof.util.Utility;

public class AllProjectBuilder extends AbstractListBuilder implements
		OnClickListener {

	private int mWidth;
	protected int mSelectedPosition = -1;

	public AllProjectBuilder(Context context, ViewGroup parent) {
		super(context, parent);

		mWidth = mContext.getResources().getDisplayMetrics().widthPixels;

		mWidth = mWidth + mWidth / 3;
	}

	@Override
	public void notifyDataChange() {
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.header_deal_item:
			int position = (Integer) v.getTag();
			Project project = (Project) mData.get(position);
			Intent intent = new Intent(mContext, DetailProject.class);

			intent.putExtra(DetailProject.PROJECT_ID, project.getId());

			// intent.putExtra(DetailProject.PROJECT_LOCALID,
			// project.getLocalId());

			intent.putExtra(DetailProject.OPPORTUNITY_STATUS,
					project.getStatus());

			mContext.startActivity(intent);
			break;
		case R.id.table_row_deal:
			position = (Integer) v.getTag();
			int firstItem = mListView.getFirstVisiblePosition()
					- mListView.getHeaderViewsCount();
			int real_position = position - firstItem;
			if (real_position >= 0 && real_position < mListView.getChildCount()) {
				View layout_item = mListView.getChildAt(real_position);
				View menu = layout_item.findViewById(R.id.deal_menu);
				menu.setVisibility(menu.getVisibility() == View.VISIBLE ? View.GONE
						: View.VISIBLE);
				mSelectedPosition = menu.getVisibility() == View.VISIBLE ? position
						: -1;
				notifyDataChange();
			}
			break;
		default:
			break;
		}

	}

	@Override
	public View buildConvertView() {
		View layout = mLayoutInflater.inflate(R.layout.deal_item, null);
		TableRow tableRow = (TableRow) layout.findViewById(R.id.table_row_deal);
		tableRow.setOnClickListener(this);
		layout.findViewById(R.id.header_deal_item).setOnClickListener(this);

		for (int i = 0; i < tableRow.getChildCount(); i++) {
			View child = tableRow.getChildAt(i);
			child.getLayoutParams().width = mWidth / 4;
		}

		return layout;
	}

	@Override
	public ConcurrentHashMap<Integer, View> buildConvertViewContent(View base) {

		ConcurrentHashMap<Integer, View> views = new ConcurrentHashMap<Integer, View>();

		views.put(R.id.table_row_deal, base.findViewById(R.id.table_row_deal));

		views.put(R.id.deal_menu, base.findViewById(R.id.deal_menu));

		views.put(R.id.opportunity_name,
				base.findViewById(R.id.opportunity_name));

		views.put(R.id.opportunity_status,
				base.findViewById(R.id.opportunity_status));

		views.put(R.id.customer_name, base.findViewById(R.id.customer_name));

		views.put(R.id.total_estimated_amount,
				base.findViewById(R.id.total_estimated_amount));

		views.put(R.id.close_date, base.findViewById(R.id.close_date));

		views.put(R.id.header_deal_item,
				base.findViewById(R.id.header_deal_item));

		return views;
	}

	@Override
	public void builConvertViewContentData(int position,
			ConcurrentHashMap<Integer, View> views, List<? extends Object> data) {

		Project project = (Project) mData.get(position);

		View datarow = views.get(R.id.table_row_deal);
		datarow.setBackgroundColor(mContext.getResources().getColor(
				Utility.getStatusBGColor(project.getStatus())));

		((TextView) views.get(R.id.opportunity_name))
				.setText(project.getName());

		((TextView) views.get(R.id.opportunity_status)).setText(project
				.getStatus());

		((TextView) views.get(R.id.customer_name)).setText(project
				.getCustomerCompanyName());

		((TextView) views.get(R.id.total_estimated_amount)).setText(Integer
				.toString(project.getAmount()));

		String date = DateFormat.getDateFormat(mContext).format(
				new Date(project.getDate()));
		((TextView) views.get(R.id.close_date)).setText(date);

		views.get(R.id.table_row_deal).setTag(position);

		views.get(R.id.header_deal_item).setTag(position);

		views.get(R.id.deal_menu).setVisibility(
				mSelectedPosition == position ? View.VISIBLE : View.GONE);

	}
}
